﻿using System;
using System.Text;

namespace HigLabo.Net.WindowsLive
{
    /// <summary>
    /// 
    /// </summary>
    public class CreateEventCommand : WindowsLiveCommand
    {
        /// <summary>
        /// 
        /// </summary>
        public String Name { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String Description { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public DateTimeOffset StartTime { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public DateTimeOffset EndTime { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public String Location { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Boolean IsAllDayEvent { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public AvailabilityStatus Availability { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public VisibilityStatus Visibility { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public String UserID { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public override string GraphApiPath
        {
            get
            {
                if (HttpClient.IsNullOrWhiteSpace(this.UserID))
                    return "me/events";
                else
                    return this.UserID + "/events";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public override HttpMethodName MethodName
        {
            get
            {
                return HttpMethodName.Post;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userID"></param>
        public CreateEventCommand(String userID)
        {
            this.UserID = userID;
            this.ContentType = "application/json";
            //set default 
            this.IsAllDayEvent = false;
            this.Availability = AvailabilityStatus.Free;
            this.Visibility = VisibilityStatus.Public;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override byte[] SetData()
        {
            if (HttpClient.IsNullOrWhiteSpace(Name))
                throw new ArgumentException();
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("{");
            sb.AppendFormat("name: \"{0}\"", Name);
            sb.Append(",\r\n");
            sb.AppendFormat("description: \"{0}\"", Description);
            sb.Append(",\r\n");
            sb.AppendFormat("start_time: \"{0}\"", StartTime.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"));
            sb.Append(",\r\n");
            sb.AppendFormat("end_time: \"{0}\"", EndTime.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"));
            sb.Append(",\r\n");
            sb.AppendFormat("location: \"{0}\"", Location);
            sb.Append(",\r\n");
            sb.AppendFormat("is_all_day_event: {0}", IsAllDayEvent.ToString().ToLower());
            sb.Append(",\r\n");
            sb.AppendFormat("availability: \"{0}\"", Availability.ToString().ToLower());
            sb.Append(",\r\n");
            sb.AppendFormat("visibility: \"{0}\"", Visibility.ToString().ToLower());
            sb.Append("\r\n}");

            return Encoding.UTF8.GetBytes(sb.ToString());
        }
    }
}
